<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.0","exturl":false,"sidebar":{"position":"right","display":"hide","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="引用Microsoft.Office.Interop.Outlook.dll 添加using using Outlook &#x3D; Microsoft.Office.Interop.Outlook; using Microsoft.Office.Interop.Outlook;">
<meta property="og:type" content="article">
<meta property="og:title" content="C#操作Outlook基本常规语句">
<meta property="og:url" content="http://example.com/fa1d6ed9.html">
<meta property="og:site_name" content="Tony Liu 的博客">
<meta property="og:description" content="引用Microsoft.Office.Interop.Outlook.dll 添加using using Outlook &#x3D; Microsoft.Office.Interop.Outlook; using Microsoft.Office.Interop.Outlook;">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2013-01-14T04:11:47.000Z">
<meta property="article:modified_time" content="2023-01-05T07:06:37.553Z">
<meta property="article:author" content="Tony Liu">
<meta property="article:tag" content="Outlook">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/fa1d6ed9.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/fa1d6ed9.html","path":"fa1d6ed9.html","title":"C#操作Outlook基本常规语句"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>C#操作Outlook基本常规语句 | Tony Liu 的博客</title>
  






  <script async defer data-website-id="" src=""></script>

  <script defer data-domain="" src=""></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Tony Liu 的博客</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">为所当为 顺其自然</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Tony Liu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">4</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/fa1d6ed9.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Tony Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tony Liu 的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="C#操作Outlook基本常规语句 | Tony Liu 的博客">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          C#操作Outlook基本常规语句
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2013-01-14 12:11:47" itemprop="dateCreated datePublished" datetime="2013-01-14T12:11:47+08:00">2013-01-14</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Outlook/" itemprop="url" rel="index"><span itemprop="name">Outlook</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>引用Microsoft.Office.Interop.Outlook.dll</p>
<p>添加using</p>
<pre class="line-numbers language-c#" data-language="c#"><code class="language-c#">using Outlook &#x3D; Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<span id="more"></span>

<p>创建Application</p>
<pre class="line-numbers language-c#" data-language="c#"><code class="language-c#">Outlook.Application myOutlookApp &#x3D; new Outlook.Application();
Outlook.NameSpace myNameSpace &#x3D; myOutlookApp.GetNamespace(&quot;MAPI&quot;);<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>本地邮箱</p>
<pre class="line-numbers language-c#" data-language="c#"><code class="language-c#">Outlook.MAPIFolder myFolderInbox &#x3D; myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
&#x2F;&#x2F;获取收件箱对象，如获取其他箱可在参数中控制
Outlook.MAPIFolder myFolder &#x3D; myFolderInbox.Folders[&quot;xx&quot;];
&#x2F;&#x2F;“xx”为收件箱下的一个文件夹
Outlook.MAPIFolder MyParentFolder &#x3D; myFolderInbox.Parent as Outlook.MAPIFolder;
&#x2F;&#x2F;获取收件箱上一级的文件夹对象，以次来获取与收件箱同级的文件夹
Outlook.MAPIFolder MyNewFolder &#x3D; MyParentFolder.Folders[&quot;yy&quot;];&#x2F;&#x2F;“yy”为与收件箱同级的文件夹<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>公共邮箱</p>
<pre class="line-numbers language-c#" data-language="c#"><code class="language-c#">string publicMailBoxName&#x3D;&quot;jpn_eco_ci04@hp.com&quot;;
Outlook.Recipient objRecipient &#x3D; myNameSpace.CreateRecipient(publicMailBoxName);&#x2F;&#x2F;jpn_eco_ci04@hp.com为公共邮箱地址的全名称
Outlook.MAPIFolder myFolderInbox &#x3D; myNameSpace.GetSharedDefaultFolder(objRecipient, OlDefaultFolders.olFolderInbox);
Outlook.MAPIFolder myFolder &#x3D; myFolderInbox.Parent as Outlook.MAPIFolder;
Outlook.MAPIFolder myNewFolder &#x3D; myFolder.Folders[&quot;(Z) 処理済&quot;];

&#x2F;&#x2F;指定发送的邮箱为公共邮箱
string publicMailBoxName &#x3D; &quot;vpchi.dlbc@hp.com&quot;;
mail.SentOnBehalfOfName &#x3D; publicMailBoxName;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>筛选</p>
<pre class="line-numbers language-c#" data-language="c#"><code class="language-c#">&#x2F;&#x2F;根据Subject属性对邮件进行筛选
const string subject &#x3D; &quot;BIS B2B Document Delivery&quot;;
string sFilter &#x3D; &quot;@SQL&#x3D;&quot; + &quot;\&quot;&quot; + &quot;urn:schemas:httpmail:subject&quot; + &quot;\&quot;&quot; + &quot; like &#39;%&quot; + subject + &quot;%&#39;&quot;;
&#x2F;&#x2F;根据邮件的收取时间进行筛选
Outlook.Items myMails &#x3D; myFolderInbox.Items.Restrict(sFilter) as Outlook.Items;
&#x2F;&#x2F;通配符
通配符
%
包含零个或多个字符的任意字符串。
WHERE title LIKE &#39;%computer%&#39; 将查找在书名中任意位置包含单词 &quot;computer&quot; 的所有书名。
_（下划线）
任何单个字符。
WHERE au_fname LIKE &#39;_ean&#39; 将查找以 ean 结尾的所有 4 个字母的名字（Dean、Sean 等）。
[ ]
指定范围 ([a-f]) 或集合 ([abcdef]) 中的任何单个字符。
WHERE au_lname LIKE &#39;[C-P]arsen&#39; 将查找以 arsen 结尾并且以介于 C 与 P 之间的任何单个字符开始的作者姓氏，例如 Carsen、Larsen、Karsen 等。在范围搜索中，范围包含的字符可能因排序规则的排序规则而异。
[^]
不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。
WHERE au_lname LIKE &#39;de[^l]%&#39; 将查找以 de 开始并且其后的字母不为 l 的所有作者的姓氏。
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>获取邮件对象、根据筛选条件移动邮件、获取附件并将其存到本地磁盘</p>
<pre class="line-numbers language-c#" data-language="c#"><code class="language-c#">const string filePath &#x3D; @&quot;C:\Print Attachment Folder\&quot;;
if (MyMails.Count !&#x3D; 0)
&#123;
    do
    &#123;
        Outlook.MailItem myMail &#x3D; myMails[1] as Outlook.MailItem;
        Outlook.Attachments myAttachments &#x3D; myMail.Attachments as Outlook.Attachments;
        foreach (Outlook.Attachment myAttachment in myAttachments)
        &#123;
            myAttachment.SaveAsFile(filePath + myAttachment.FileName);
        &#125;
        myMail.Move(MyMoveFolder);
        myMails &#x3D; MyFolderInbox.Items.Restrict(sfilter) as Outlook.Items;
    &#125;
    while (myMails.Count !&#x3D; 0);
&#125;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>创建新邮件并添加附件发送</p>
<pre class="line-numbers language-c#" data-language="c#"><code class="language-c#">Outlook.Application myOutlookApp &#x3D; new Outlook.Application();
Outlook.MailItem mail &#x3D; myOutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Attachments.Add(TemplateExcel, Outlook.OlAttachmentType.olEmbeddeditem, 1, System.Type.Missing);
mail.Subject &#x3D; &quot;QC Check File&quot;;
string to &#x3D; string.Empty; ;
for (int i &#x3D; 0; i &lt; mails.Count; i++)
&#123;
     to &#x3D; to + mails[i] + &quot;;&quot;;
&#125;
mail.To &#x3D; to;
mail.Send();<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p>CSDN博客地址：<a target="_blank" rel="noopener" href="https://blog.csdn.net/yanlovehan/article/details/8500449">https://blog.csdn.net/yanlovehan/article/details/8500449</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Outlook/" rel="tag"># Outlook</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/4ac4d4ca.html" rel="prev" title="C#操作Excel基本常规语句">
                  <i class="fa fa-chevron-left"></i> C#操作Excel基本常规语句
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/4ecb35f5.html" rel="next" title="Python itchat个人微信账号接口定时发送群消息">
                  Python itchat个人微信账号接口定时发送群消息 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2012 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tony Liu</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  





</body>
</html>
